import 'dart:convert';
import 'package:get/get.dart';
import '/router/router.dart';
import 'package:flutter/material.dart';
import 'package:shared_preferences/shared_preferences.dart';

class MenuComponent extends StatefulWidget {
  MenuComponent({Key? key}) : super(key: key);

  @override
  _MenuComponentState createState() => _MenuComponentState();
}

class _MenuComponentState extends State<MenuComponent> {
  late SharedPreferences prefs;
  late Map<String, dynamic> loginInfo = {};
  late String username = '';
  late List appMenus = [];
  late List ownerAppMenus;
  late String userAvatar =
      'https://ss1.bdstatic.com/70cFvXSh_Q1YnxGkpoWK1HF6hhy/it/u=3726493241,3548681058&fm=11&gp=0.jpg';
  late List departs;
  Future<void> initLoginInfoState() async {
    prefs = await SharedPreferences.getInstance();
    if (prefs.getString('loginInfo') != null) {
      loginInfo = json.decode(prefs.getString('loginInfo')!);
      departs = loginInfo['departs']!;
      setState(() {
        username = loginInfo['realname']!;
        appMenus = loginInfo['appMenus']!;
        ownerAppMenus =
            appMenus.where((element) => element['ifOcApply']).toList();
        userAvatar = loginInfo['avatar']!;
      });
    }
  }

  @override
  void initState() {
    super.initState();
    initLoginInfoState();
  }

  @override
  Widget build(BuildContext context) {
    return CustomScrollView(
      slivers: <Widget>[
        SliverAppBar(
          expandedHeight: 200,
          pinned: true,
          leading: Container(
            padding: EdgeInsets.fromLTRB(10, 5, 5, 5),
            child: ClipRRect(
              borderRadius: BorderRadius.circular(20),
              child: Image.network(
                userAvatar,
                width: 15,
                height: 15,
              ),
            ),
          ),
          title: Text(
            username,
            style: TextStyle(
              fontSize: 16.0, // 文字大小
            ),
          ),
          flexibleSpace: FlexibleSpaceBar(
            background: Image.network(
              'https://gimg2.baidu.com/image_search=http%3A%2F%2Fpng.pngtree.com%2Fthumb_back%2Ffh260%2Fbackground%2F20190221%2Fourmid%2Fpngtree-2-5d-office-order-simulation-character-image_17858.jpg&refer=http%3A%2F%2Fpng.pngtree.com&app=2002&size=f9999,10000&q=a80&n=0&g=0n&fmt=jpeg?sec=1626594956&t=a80c9e11e3a8142aea6ffdbf751a86e1',
              fit: BoxFit.cover,
            ),
          ),
          actions: [
            MaterialButton(
              // color: Colors.deepPurple,
              textColor: Colors.deepPurple,
              child: Text('登录'),
              onPressed: () {
                Get.toNamed(RouteConfig.login);
              },
            ),
          ],
        ),
        SliverPadding(
          padding: EdgeInsets.all(20),
          sliver: SliverGrid(
            gridDelegate: SliverGridDelegateWithFixedCrossAxisCount(
              ///九宫格的列数
              crossAxisCount: 4,

              ///子Widget 宽与高的比值
              childAspectRatio: 1.3,

              ///主方向的 两个 子Widget 之间的间距
              mainAxisSpacing: 10,

              /// 次方向 子Widget 之间的间距
              crossAxisSpacing: 10,
            ),

            ///子Item构建器
            delegate: new SliverChildBuilderDelegate(
              (BuildContext context, int index) {
                ///每一个子Item的样式
                return InkWell(
                  onTap: () {
                    print(appMenus[index]['hasRight']);
                    Get.toNamed("/" + appMenus[index]['localIconName']);
                    // if (appMenus[index]['hasRight']) {
                    //   Get.toNamed(appMenus[index]['localIconName']);
                    // } else {}
                  },
                  child: Column(
                      crossAxisAlignment: CrossAxisAlignment.center,
                      mainAxisAlignment: MainAxisAlignment.center,
                      children: <Widget>[
                        Container(
                          width: 30,
                          height: 30,
                          child: Image.network(appMenus[index]['icon']),
                        ),
                        Text(appMenus[index]['title'])
                      ]),
                );
              },

              ///子Item的个数
              childCount: appMenus.length,
            ),
          ),
        ),
      ],
    );
  }
}
